home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / Integer.st < prev    next >
Text File  |  1991-09-12  |  5KB  |  260 lines

  1. "======================================================================
  2. |
  3. |   Integer Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         12 Sep 91      Fixed gcd: to return result instead of self.
  34. |
  35. | sbyrne     25 Apr 90      Fixed (oh...happy birthday, Integer.st!) bitInvert.
  36. |              After fixing the lexer to be pickier about integer
  37. |              literals that were too large to be represented as
  38. |              Smalltalk literals, the previous code (which xored
  39. |              with 7fffffff) broke, so as a hack I computed two's
  40. |              complement and subtracted one.
  41. |
  42. | sbyrne     25 Apr 89      created.
  43. |
  44. "
  45.  
  46. Number variableWordSubclass: #Integer "### Not really a variable word subclass"
  47.        instanceVariableNames: ''
  48.        classVariableNames: ''
  49.        poolDictionaries: ''
  50.        category: nil.
  51.  
  52. Integer comment:
  53. 'I am the integer class of the GNU Smalltalk system.  My instances can 
  54. represent 31 bit integers and are as efficient as possible.' !
  55.  
  56. !Integer methodsFor: 'Misc math operators'!
  57.  
  58. hash
  59.     ^self
  60. !!
  61.  
  62.  
  63.  
  64. !Integer methodsFor: 'Other iterators'!
  65.  
  66. timesRepeat: aBlock
  67.     | i |
  68.     i _ 1.
  69.     [ i <= self ] whileTrue: [ aBlock value.
  70.                                i _ i + 1 ]
  71. !!
  72.  
  73.  
  74.  
  75. !Integer methodsFor: 'bit operators'!
  76.  
  77. bitAt: index
  78.     ^(self bitShift: index negated) bitAnd: 1
  79. !
  80.  
  81. bitInvert
  82.     "Return the 1's complement of the bits of the receiver"
  83.     ^self negated - 1        "compute 2's complement then remove 1"
  84. !
  85.  
  86.  
  87. allMask: anInteger
  88.     "True if all bits in anInteger are 1 in the receiver"
  89.     ^(self bitAnd: anInteger) = anInteger
  90. !
  91.  
  92. anyMask: anInteger
  93.     "True if any 1 bits in anInteger are 1 in the receiver"
  94.     ^(self bitAnd: anInteger) ~= 0
  95. !
  96.  
  97. noMask: anInteger
  98.     "True if no 1 bits in anInteger are 1 in the receiver"
  99.     ^(self bitAnd: anInteger) = 0
  100. !
  101.  
  102. highBit
  103.     "Return the index of the highest order 1 bit of the receiver"
  104.     self = 0 ifTrue: [ ^-1 ].    "??? I don't know what the right value is"
  105.     30 to: 1 step: -1 do:
  106.         [ :i | (self bitAnd: (1 bitShift: i)) ~= 0 ifTrue: [ ^i ] ]
  107. !!
  108.  
  109.  
  110.  
  111. !Integer methodsFor: 'Math methods'!
  112.  
  113. factorial
  114.     self < 2 ifTrue: [ ^1 ]
  115.              ifFalse: [ ^self * (self - 1) factorial ]
  116. !
  117.  
  118. gcd: anInteger
  119.     | selfInteger temp |
  120.     "Return the greatest common divisor (Euclid's algorithm)"
  121.     selfInteger _ self.
  122.     [ anInteger ~= 0 ]
  123.         whileTrue: [ temp _ selfInteger \\ anInteger.
  124.                  selfInteger _ anInteger.
  125.              anInteger _ temp. ].
  126.     ^selfInteger
  127. !
  128.  
  129. lcm: anInteger
  130.     ^(self * anInteger) abs // (self gcd: anInteger)
  131. !
  132.  
  133. even
  134.     ^(self bitAnd: 1) = 0
  135. !
  136.  
  137. odd
  138.     ^(self bitAnd: 1) ~= 0
  139. !!
  140.  
  141.  
  142.  
  143. !Integer methodsFor: 'Coercion methods (heh heh heh)'!
  144.  
  145. asCharacter
  146.     "Return self as an ascii character"
  147.     (self <= 255 and: [ self >= 0])
  148.         ifTrue: [ ^Character value: self ]
  149.     ifFalse: [ ^self error: 'Integer not convertible to character' ]
  150. !
  151.  
  152. coerce: aNumber
  153.     ^aNumber truncated
  154. !
  155.  
  156. generality
  157.     ^1
  158. !
  159.  
  160. ceiling
  161.     ^self
  162. !
  163.  
  164. floor
  165.     ^self
  166. !
  167.  
  168. truncated
  169.     ^self
  170. !
  171.  
  172. rounded
  173.     ^self
  174. !!
  175.  
  176.  
  177.  
  178.  
  179. !Integer methodsFor: 'copying'!
  180.  
  181. shallowCopy
  182.     ^self
  183. !
  184.  
  185. deepCopy
  186.     ^self
  187. !!
  188.  
  189.  
  190.  
  191.  
  192. !Integer methodsFor: 'printing'!
  193.  
  194. printOn: aStream base: b
  195.     aStream nextPutAll: (self radix: b)
  196. !
  197.  
  198. radix: baseInteger
  199.     ^self signedStringBase: baseInteger showRadix: true
  200. !
  201.  
  202. printOn: aStream
  203.     (self signedStringBase: 10 showRadix: false) printOn: aStream
  204. !!
  205.  
  206.  
  207.  
  208. !Integer methodsFor: 'storing'!
  209.  
  210. storeOn: aStream
  211.     self printOn: aStream        "they print and store the same"
  212. !!
  213.  
  214.  
  215.  
  216. !Integer methodsFor: 'not implemented'!
  217.  
  218. asFraction
  219.     self notYetImplemented
  220. !!
  221.  
  222.  
  223.  
  224. !Integer methodsFor: 'private'!
  225.  
  226. signedStringBase: baseInteger showRadix: showRadix
  227.     | str revString string sign len num i |
  228.     self < 0
  229.         ifTrue: [ sign _ true.
  230.               num _ self negated ]
  231.     ifFalse: [ sign _ false.
  232.                num _ self ].
  233.     revString _ num revDigitsBase: baseInteger.
  234.     str _ WriteStream on: (String new: 1).
  235.     showRadix ifTrue:
  236.         [ baseInteger printOn: str.
  237.       str nextPut: $r ].
  238.     sign ifTrue: [ str nextPut: $- ].
  239.     revString reverseDo:
  240.         [ :char | str nextPut: char ].
  241.     ^str contents
  242. !
  243.  
  244. revDigitsBase: b    
  245.     | str num |
  246.     str _ WriteStream on: (String new: 1).
  247.     self = 0
  248.         ifTrue: [ str nextPut: $0 ]
  249.     ifFalse: [
  250.             num _ self.
  251.         [ num = 0 ] whileFalse:
  252.             [ str nextPut: (Character digitValue: num \\ b).
  253.           num _ num // b ] ].
  254.     ^str contents
  255.  
  256. !!
  257.  
  258.